/*
 * queue.h
 *
 *  Created on: Sep 12, 2012
 *      Author: arun
 */

#ifndef QUEUE_H_
#define QUEUE_H_

#include <stdlib.h>

struct data_t
{
	long packet_id;
	int size;
	long service_time;
	long arrival_time;
	long waiting_time;
};

struct node_t
{
	struct data_t data;
	struct node_t* next;
};

struct queue_t
{
	struct node_t* front, *rear;
	long node_count;
};

bool enqueue(struct queue_t *q, struct data_t new_data){
	try
	{
		struct node_t* new_node = (struct node_t*) malloc (sizeof(struct node_t));
		new_node->data = new_data;
		new_node->next = NULL;
		if(q->front == NULL && q->rear == NULL){
			q->front = q->rear = new_node;
		}
		else
		{
			//struct node_t *prev, *cur;
			//cur = q->front;
			q->rear->next = new_node;
			q->rear = new_node;
		}
		q->node_count++;
	}
	catch(...)
	{
		return false;
	}
	return true;
}

struct data_t dequeue(struct queue_t *q)
{
	struct data_t data;
	data.packet_id = -1;
	if(q->front == NULL){
		return data;
	}
	else
	{
		data = q->front->data;
		struct node_t *del = q->front;
		q->front = q->front->next;
		free(del);
		q->node_count--;
	}
	return data;
}


void sort_queue(struct queue_t *q)
{

}

void init_queue(struct queue_t *q){
	q->front = NULL;
	q->rear = NULL;
	q->node_count = 0;
}



#endif /* QUEUE_H_ */
